#include "HttpServer.hpp"

#include <memory>

using namespace std;
using namespace server;

void Usage(string proc)
{
    cerr << "\nUsage:\n\t" << proc << " serverport\r\n\r\n";
}
std::string suffixToDesc(const std::string suffix)
{
    std::string ct = "Content-Type: ";
    if (suffix == ".html")
        ct += "text/html";
    else if (suffix == ".jpg")
        ct += "image/jpeg";
    else if (suffix == ".css")
        ct += "text/css";
    else if (suffix == ".png")
        ct += "image/png";
    else if (suffix == ".mp4")
        ct += "video/mpeg4";
    else if (suffix == ".jpeg")
        ct += "image/jpeg";
    ct += "\r\n";
    return ct;
}
// 服务器和网页分离
// url->/:web根目录
// 我们要正确的给客户端返回资源类型，我们首先要自己知道，所有的资源都有后缀
bool Get(const HttpRequest &req, HttpResponse &resp)
{
    if (req.path == "text.py")
    {
        // 建立进程间通信,pipe
        // 进行fork创建子进程 execl("/bin/python",text.py)
        // 父进程将req.parm通过管道写给某些后端语言，py,java等
    }
    if (req.path == "/search")
    {
        // 使用我们自己写的c++方法，提供服务
    }
    cout << "---------------http start------------------------" << std::endl;
    cout << req.inbuffer << endl;
    std::cout << "method: " << req.method << std::endl;
    std::cout << "url: " << req.url << std::endl;
    std::cout << "httoversion: " << req.httpversion << std::endl;
    std::cout << "path: " << req.path << std::endl;
    std::cout << "suffix: " << req.suffix << std::endl;
    std::cout << "size: " << req.size <<"字节"<< std::endl;

    cout << "---------------http end--------------------------" << std::endl;
    std::string respline = "HTTP/1.1 200 Ok\r\n";
    std::string respheader = suffixToDesc(req.suffix);
    if (req.size > 0)
    {
        // respheader += "Content-Length: ";
        // respheader += std::to_string(req.size);
        // respheader += "\r\n";
    }

    respheader+="Set-Cookie: name=123456789abcdefg; Max-Age=120\r\n";
    // respheader+="Location: https://www.qq.com/\r\n";
    std::string respblank = "\r\n";
    // std::string body="<html lang=\"en\"><head><meta charset=\"UTF-8\"><title>服务器测试</title><h1>希望你不会挂科</h1></head><body><p>哈哈哈哈哈啊哈</p></body><h2>吼吼吼吼</h2></html>";
    std::string body;
    body.resize(req.size+1);
    if (!Util::readFile(req.path, (char*)body.c_str(),req.size))
    {
        Util::readFile(html_404, (char*)body.c_str(),req.size); // 一定能成功
    }
       respheader += "Content-Length: ";
        respheader += std::to_string(body.size());
        respheader += "\r\n";
    resp.outbuffer += respline;
    resp.outbuffer += respheader;
    resp.outbuffer += respblank;
    
    cout << "---------------http response start--------------------------" << std::endl;
    std::cout  << resp.outbuffer << std::endl;
    cout << "---------------http response end--------------------------" << std::endl;


    resp.outbuffer += body;
    return true;
}

// ./tcpclient serverip serverport

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        Usage(argv[0]);
        exit(0);
    }
    uint16_t port = atoi(argv[1]);
    unique_ptr<HttpServer> httpsvr(new HttpServer(Get, port));
    // httpsvr->registerCb("/", Get);
    // httpsvr->registerCb("/search", Search);
    // httpsvr->registerCb("test.py", Other);

    httpsvr->initServer();
    httpsvr->start();
    return 0;
}